/*********************************************************************************
 *      Copyright:  (C) 2024 LiYi<1751425323@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  socket_pack.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(18/04/24)
 *         Author:  LiYi <1751425323@qq.com>
 *      ChangeLog:  1, Release initial version on "18/04/24 22:14:22"
 *                 
 ********************************************************************************/

#include "socket_pack.h"
#include "netdb.h"

int socket_init(socket_ctx_t *sock,char *host,int port)
{
	if(!sock || port <= 0)
		return -1;

	//memset(sock,0,sizeof(*sock));
	sock->fd = -1;
	sock->port=port;

	if(host)
	{
		strncpy(sock->host,host,strlen(host));
	}
	else
	{
		return -1;
	}

	return 0;
}	
#if 0
//no DNS
int sock_conncet(socket_ctx_t *sock)
{
	struct sockaddr_in serv_addr;
	int    connect_flag=0;
	if(!connect_flag)
	{
		if((sock->fd = socket(AF_INET,SOCK_STREAM,0)) < 0)
		{
			log_error("socket fd failure:%s",strerror(errno));
			return -2;
		}

		memset(&serv_addr,0,sizeof(serv_addr));
		serv_addr.sin_family = AF_INET;
		serv_addr.sin_port   = htons(sock->port);
		inet_aton(sock->host,&serv_addr.sin_addr);

		if((connect(sock->fd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))) < 0)
		{
			log_error("connect failure : %s\n",strerror(errno));
			close(sock->fd);
			connect_flag = 0;
		}
		else 
		{
			connect_flag = 1;
		}

	}
	return connect_flag;

}
#endif

int sock_conncet(socket_ctx_t *sock)
{
	int					connect_flag = 0;
	int 				sockfd = 0;
	char 				service[20];
	struct	addrinfo	hints,*rp;
	struct	addrinfo	*res = NULL;
	struct	in_addr		inaddr;
	struct	sockaddr_in	addr;
	int					len = sizeof(addr);
	int 				rc;

	if(!sock)
		return -1;

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family 	= AF_INET;
	hints.ai_socktype	= SOCK_STREAM;
	hints.ai_protocol	= IPPROTO_TCP;


	if( inet_aton(sock->host,&inaddr) )
	{
		hints.ai_flags |= AI_NUMERICHOST;
	}


	snprintf(service,sizeof(service),"%d",sock->port);
	if(rc = getaddrinfo(sock->host,service,&hints,&res)) //0正确
		{
			log_error("getaddrinfo() parser [%s:%s] failed :%s\n",sock->fd,service,gai_strerror(rc));
			return -3;
		}


for(rp=res; rp!=NULL;rp=rp->ai_next)
{

	sockfd = socket(rp->ai_family,rp->ai_socktype,rp->ai_protocol);
	if(sockfd < 0)
	{
		log_error("socket() create failure :%s\n",strerror(errno));
		continue;
	}

	if(connect(sockfd,rp->ai_addr,len) < 0)
	{

		log_error("connect() failure :%s\n",strerror(errno));
		close(sockfd);
		connect_flag=0;
		continue;		
	}
	else
	{
		sock->fd = sockfd;
		connect_flag = 1;
		break;
	}
}

freeaddrinfo(res);
return connect_flag;

}


int socket_write(int connfd,char *buf,int bytes)
{
	int num = 0;
	num = write(connfd,buf,bytes);
	return num;
}

int socket_read(int connfd,char *buf,int bytes)
{
	int num = 0;
	num = read(connfd,buf,bytes);

	return num;
}

int  socket_close(socket_ctx_t *sock)
{
	if(sock->fd == -1)
	{
		return -1;
	}

	close(sock->fd);
	sock->fd = -1;
}



